package com.maochang.top.dao.impl;


import com.maochang.top.bean.TUser;
import com.maochang.top.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int add(TUser user) {
        String sql="insert into t_book values(?,?,?,?)";
        int update = jdbcTemplate.update(sql, null, user.getUsername(), user.getPassword(), user.getEmail());
        return update;
    }

    @Override
    public TUser getByName(String userName) {
        String sql="select * from t_user where username=?";
        TUser admin = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(TUser.class), "admin");
        return admin;
    }

    @Override
    public int delByName(String name) {
        String sql="delete from  t_user where username=?";
        int update = jdbcTemplate.update(sql, name);
        return update;
    }

    @Override
    public int update(TUser user) {
        String sql="update t_user set password=? where username=?";
        int update = jdbcTemplate.update(sql, user.getPassword(), user.getUsername());
        return update;

    }
    @Override
    public List<TUser> getUserList() {
        String sql="select * from t_user";
        List<TUser> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TUser.class));
        return query;
    }

}
